{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 加法\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd2510b4-eaf7-47a0-a42a-dfe9168b828d",
   "metadata": {},
   "source": [
    "该代码通过用户输入两个数字，将它们相加并输出结果。它使用Python的`input`函数获取用户输入的数字，并进行类型转换、加法运算和格式化输出。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **用户输入**：程序首先提示用户输入两个数字，存储为变量`num1`和`num2`。输入值默认以字符串形式保存。\n",
    "\n",
    "2. **类型转换与求和**：代码使用`float()`函数将字符串形式的输入值转换为浮点数，以便进行加法运算。假设用户输入的两个数分别为$x$和$y$，加法计算如下：\n",
    "\n",
    "   $$\n",
    "   \\text{sum\\_2\\_nums} = x + y\n",
    "   $$\n",
    "\n",
    "3. **结果显示**：代码通过格式化字符串将输入的两个数字及其和插入到输出字符串中。假设用户输入的数字为$x = 2.5$和$y = 3.7$，则输出结果为：\n",
    "\n",
    "   $$\n",
    "   \\text{The sum of 2.5 and 3.7 is 6.2}\n",
    "   $$\n",
    "\n",
    "### 总结\n",
    "\n",
    "此代码展示了如何将用户输入的字符串转换为浮点数，并通过加法运算获取和。它使用格式化输出方式将计算结果和输入值一起呈现出来，适用于动态地获取用户输入、处理数值计算并输出结果的情境。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad368ff4-5ad2-4309-957c-49b7f8325101",
   "metadata": {},
   "source": [
    "## 用户输入两个数字并计算其和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "36503fa8-6c5c-44c9-bca8-0ad9ef5a7f92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Enter first number:  2.5\n"
     ]
    }
   ],
   "source": [
    "num1 = input('Enter first number: ')  # 输入第一个数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "625e4637-7311-4bde-a9b7-f787e37719c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Enter second number:  3.7\n"
     ]
    }
   ],
   "source": [
    "num2 = input('Enter second number: ')  # 输入第二个数字"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6c2f1b6-d516-4bab-b55e-4d0963da3716",
   "metadata": {},
   "source": [
    "## 计算两个数字的和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a3d41ffb-2f69-4e40-8f1c-da86d2d9f1e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "sum_2_nums = float(num1) + float(num2)  # 将输入的数字转换为浮点数后求和"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b207d98-88db-4495-9d1e-3672469a85c8",
   "metadata": {},
   "source": [
    "## 显示计算结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "57418cae-3cb8-4b6a-898a-b0149564bd05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sum of 2.5 and 3.7 is 6.2\n"
     ]
    }
   ],
   "source": [
    "print('The sum of {0} and {1} is {2}'.format(num1, num2, sum_2_nums))  # 输出两个数字的和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
